import Anime from './Anime';
import {animeDelay} from './config';

var animation = {
    huge:function(panels,nv,ov){
        var l = panels.length;
        var delta = nv-ov;
        var hasChange = false;
        var i,arr;
        nv = ArrayExtend((nv + '').split(''),l);
        ov = ArrayExtend((ov + '').split(''),l);
        arr = ov.map(function(v,i){
            if(hasChange||v!=nv[i]){
                if(hasChange){
                    return new Anime(Date.now(),1500+animeDelay*(l-i),v,nv[i]+10);
                }else{
                    hasChange = true;
                    return new Anime(Date.now(),1500+animeDelay*(l-i),v,nv[i]);
                }
            }else{
                return null;
            }
        })
        panels.forEach(function(panel,i){
            panel.reciveAnime(arr[i]);
        })
    },
    small:function(panels,nv,ov){
        var l = panels.length;
        var delta = nv-ov;
        var hasChange = false;
        var i,arr;
        nv = ArrayExtend((nv + '').split(''),l);
        ov = ArrayExtend((ov + '').split(''),l);
        if(delta>0){
            arr = ov.map(function(v,i){
                if(hasChange||v!=nv[i]){
                    if(hasChange){
                        return new Anime(Date.now(),1000+animeDelay*(l-i),v,nv[i]+40);
                    }else{
                        hasChange = true;
                        return new Anime(Date.now(),1000+animeDelay*(l-i),v,nv[i]);
                    }
                }else{
                    return null;
                }
            })
        }else{
            arr = ov.map(function(v,i){
                if(hasChange||v!=nv[i]){
                    hasChange = true;
                    return new Anime(Date.now(),1000+animeDelay*(l-i),v<nv[i]?v:(v+10),nv[i]);
                }else{
                    return null;
                }
            })
        }
        panels.forEach(function(panel,i){
            panel.reciveAnime(arr[i]);
        })
    }
}

var ArrayExtend = function(arr,length){
    var l = length - arr.length;
    l = l>0?l:0;
    return Array.fill(Array(l), 0).concat(arr).map(function(v){return +v});
}

export default animation;